package com.hejjon;

/**
 *
 *  63. 股票的最大利润
 *      https://leetcode.cn/problems/gu-piao-de-zui-da-li-run-lcof/
 * @author: caoshi
 * @date: 2023-02-02 20:58:21
 * @since: 1.0
 */
public class MaxProfit {

    public static void main(String[] args) {
//        int[] prices = new int[]{7, 1, 5, 3, 6, 4};
        int[] prices = new int[]{7, 1, 4, 9, 1, 10};
        int res = new MaxProfit().maxProfit(prices);

        System.out.println(res);
    }

    public int maxProfit(int[] prices) {

        if (prices == null || prices.length == 0) {
            return 0;
        }

        int maxProfit = 0;
        int lowest = prices[0];

        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > lowest) {
                maxProfit = Math.max(prices[i] - lowest, maxProfit);
            } else {
                lowest = prices[i];
            }

        }

        return maxProfit;
    }


}
